﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using HtmlAgilityPack;
using HaoLab.Library;
using GB2312;

namespace HaoLab.Library.NCEPU.Search
{
    public class SimpleSearch
    {


        List<KeyValuePair<string, string>> Params;

        Uri uri;

        const string host = "http://202.204.72.4:8080/opac/";

        public SimpleSearch()
        {

            Params = new List<KeyValuePair<string, string>>();
            Params.Add(new KeyValuePair<string, string>("dept", "ALL"));
            Params.Add(new KeyValuePair<string, string>("displaypg", "20"));
            Params.Add(new KeyValuePair<string, string>("doctype", "ALL"));
            Params.Add(new KeyValuePair<string, string>("historyCount", "1"));
            Params.Add(new KeyValuePair<string, string>("match_flag", "forward"));
            Params.Add(new KeyValuePair<string, string>("orderby", "desc"));
            Params.Add(new KeyValuePair<string, string>("showmode", "list"));
            Params.Add(new KeyValuePair<string, string>("sort", "CATA_DATE"));
            Params.Add(new KeyValuePair<string, string>("strSearchType", "title"));
            Params.Add(new KeyValuePair<string, string>("strText", "测试"));
            Params.Add(new KeyValuePair<string, string>("x", "0"));
            Params.Add(new KeyValuePair<string, string>("y", "0"));
            uri = new Uri( host+"openlink.php");

        }

        public void Find(Action<DownloadStringCompletedEventArgs> action)
        {
            WebClient wc = new WebClient();
            wc.GetHtmlForm(uri, Params);
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler((s, e) => action.Invoke(e));
        }

        public void Get()
        {

            HtmlWeb hw = new HtmlWeb();

            hw.LoadCompleted += (ss, ee) =>
                    {

                        var nodes = ee.Document.DocumentNode.SelectNodes(@"/html[1]/body[1]/body[1]/div[4]/div[1]/div[2]/div");

                        List<Book> books = new List<Book>();
                        for (int i = 2; i < nodes.Count - 1; i++)
                        {

                            Book book = new Book();
                            var node = nodes[i].SelectSingleNode(@"h3[1]");
                            book.libCode = Utility.ASCII2Unicode(node.LastChild.InnerText);
                            book.name = Utility.ASCII2Unicode(node.Element("a").InnerHtml);
                            book.href = node.Element("a").Attributes["href"].Value;

                            node = nodes[i].SelectSingleNode(@"p[1]/span[1]");
                            book.storeNum = Int16.Parse(node.ChildNodes[1].InnerText);
                            book.borrowableNum = Int16.Parse(node.ChildNodes[5].InnerText);
                            node=node.NextSibling;
                            book.author =Utility.ASCII2Unicode(node .InnerText);
                            book.press = Utility.ASCII2Unicode(node.NextSibling.NextSibling.InnerText);
                            Debug.WriteLine(book.name);
                            books.Add(book);
                            
                        }
                       
                            GetDetail(books[4]);


                    };

            hw.LoadAsync(uri.ToString(), Encoding.UTF8);
        }

        public void GetDetail (Book book)
        {
            HtmlWeb hw = new HtmlWeb();

            hw.LoadCompleted += (ss, ee) =>
            {
                var nodes = ee.Document.DocumentNode.SelectNodes(@"/html[1]/body[1]/body[1]/div[4]/div[1]/div[2]/div[1]/dl");

                foreach (var node in nodes)
                {
                    if (node.ChildNodes["dt"].InnerText == "ISBN及定价:")
                        book.ISBN = Utility.ASCII2Unicode(node.InnerText.Substring(0, node.InnerText.IndexOf("/")));
                  
                }

                
                nodes = ee.Document.DocumentNode.SelectNodes(@"/html[1]/body[1]/body[1]/div[4]/div[5]/table[1]/tr");

                for (int i = 1; i < nodes.Count; i++)
                {


                    string ID = nodes[i].ChildNodes[3].InnerText;
                    string location = nodes[i].ChildNodes[9].InnerText;
                    string isBorrowed = nodes[i].ChildNodes[11].InnerText;
                    book.conditions.Add(new Book.Condition(ID,location,isBorrowed));
                }


            };

            hw.LoadAsync(new Uri(host+book.href), Encoding.UTF8,null);
        }
    }

    public class Book
    {
        public string name;
        public string href { get; set; }
        public string libCode;
        public string author;
        public string press;
        public int storeNum;
        public int borrowableNum;
        public string ISBN { get; set; }
        public string imgSrc;
        public List<Condition> conditions = new List<Condition>();

        public class Condition
        {
            string ID;
            string location;
            string isBorrowed;
            public Condition(string ID, string location, string isBorrowed)
            {
                this.ID = ID;
                this.location = location;
                this.isBorrowed = isBorrowed;
            }
        }
    }
}
